<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/static/include/top2.jsp"%>
<script>document.title="核算指标";</script>

<script src="${baseURL}/static/js/jquery.easyui.min.js"></script>
<link href="${baseURL}/static/js/themes/default/easyui.css" rel="stylesheet" type="text/css" />

<style type="text/css">
<!--
table {
	margin:0;
	width:0;
	font-family:"微软雅黑";
	font-size:12px;
	color: #FFF;
	border-collapse: collapse;
}
//-->
</style>
<div id="context">
<table id="search_t" style="width:98%">
		<tbody>
			<tr>
				<th><img src="${ctx}/static/images/pages/cen/rig/ico_01.png" align="middle">&nbsp;核算指标
				</th>
			</tr>
			<tr class="tab_grey">
				<td>
				<form name="searchForm" action="${baseURL}/audit/queryIndicatorToPage" method="post">
					<input type="hidden" name="p" value="1" />
					<input type="hidden" id="indicatorListData" name="indicatorListData" value="">
					<input type="hidden" id="areaId" name="areaId" value="320000000000000">
					选择条件：				

				<span class="tab_text">省(自治区、直辖市):</span>

				<input type="text" class="tab_textbut" value="江苏省" readonly>

				<span class="tab_text">地(区、市、州、盟):</span>
					<select class="tab_textbut" id="areaId2" onchange="queryAreasByParent(this)">
					<option value="">请选择..</option>
					<option value="${requestScope.areasMap['320600000000000'].id}">${requestScope.areasMap['320600000000000'].areaName}</option>
					<option value="${requestScope.areasMap['320700000000000'].id}">${requestScope.areasMap['320700000000000'].areaName}</option>
					<option value="${requestScope.areasMap['320900000000000'].id}">${requestScope.areasMap['320900000000000'].areaName}</option>
				</select>



				<span class="tab_text">县(区、市、旗):</span>
				<select class="tab_textbut_100" id="areaId3">
					<option value="">请选择..</option>
				</select>

				<br/>
				<span class="tab_text">核算频率：</span>
				<select id="timeType" name="timeType" class="tab_textbut">
					<option value="">请选择..</option>
					<option value="1">年报</option>
					<option value="3">半年报</option>
					<option value="2">季报</option>
				</select>
				<select id="quarter" name="endYear" class="tab_textbut" style="display: none">
					<option value="1">第一季度</option>
					<option value="2">第二季度</option>
					<option value="3">第三季度</option>
					<option value="4">第四季度</option>
				</select>
				<span class="tab_text">核算开始年份:</span>
				<select id="startYear" name="startYear" class="tab_textbut">
					<option value="">请选择..</option>
				</select>        
		        <span class="tab_text">核算结束年份:</span>
				<select id="endYear" name="endYear" class="tab_textbut">
					<option value="">请选择..</option>
				</select>		
				
				<c:if test="${user.roleId != '6'}">
				<br/>
				<a class="tab_but02" href="#" onclick="indicatorAccount();return false;">生成核算结果</a>
				</c:if>
				<a class="tab_but02" href="#" onclick="queryData();return false;">查询核算结果</a>
				<a class="tab_but02" href="#" onclick="downIndicator();return false;">下载核算结果</a>
			</form>
				</td>
			</tr>
		</tbody>
</table>

<br>
<div id="dataList" style="overflow:auto;width:98%">
<table id="dg" class="easyui-datagrid" style="width:1400px;height:400px"   
        data-options="fitColumns:true,singleSelect:true">   
    <thead>   
        <tr>   
            <th data-options="field:'year',width:80,align:'center'">年份</th>   
            <th data-options="field:'indicatorName',width:150">指标名称</th>   
            <th data-options="field:'indicatorValue',width:120,align:'right'">指标值</th>
            <th data-options="field:'editValue',width:120,align:'right'">修正值</th>
            <th data-options="field:'indicatorUnit',width:80,align:'right'">单位</th>
            <th data-options="field:'indicatorValueGrowthRate',width:120,align:'right'">指标值增长速度</th>
            <th data-options="field:'editValueGrowthRate',width:120,align:'right'">指修正增长速度</th>
            <th data-options="field:'ivYearBookGrowthRate',width:120,align:'right'">指标值偏离值</th>
            <th data-options="field:'evYearBookGrowthRate',width:120,align:'right'">修正值偏离值</th>
            <th data-options="field:'ivLastYearBookGrowthRate',width:120,align:'right'">指标值相比增长速度</th>
            <th data-options="field:'evLastYearBookGrowthRate',width:120,align:'right'">修正值相比增长速度</th>
            <th data-options="field:'operate',width:120,align:'center'">操作</th>
        </tr>   
    </thead>   
</table>
</div>

<div id="modDiv" style="display:none">
	<div style="width:400px;height:250px">   
		<table id="listTable" style="width:98%">
			<thead>
				<tr style="display:none"><td></td></tr>
			</thead>
			<tbody>
				<tr>
					<td align="right" width="150">指标名称：</td>
					<td><span id="indicatorName"></span></td>
				</tr>
				<tr>
					<td align="right">指标核算值：</td>
					<td><span id="indicatorValue"></span></td>
				</tr>
				<tr>
					<td align="right">修改核算值为：</td>
					<td><input type="text" class="tab_textbut" id="editValue" name="editValue" value=""></td>
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2" style="text-align:center">
						<a class="tab_but" href="#" onclick="saveEditValue();return false;">保存</a>
						<a class="tab_but" href="#" onclick="winClose();return false;">关闭</a>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						全省：<span id="320000000000000"></span>
						<br/>
						南通：    <span id="320600000000000"></span>&nbsp;&nbsp;
						连云港：<span id="320700000000000"></span>&nbsp;&nbsp;
						盐城：    <span id="320900000000000"></span>
					</td>
				</tr>
			</tfoot>
		</table>
	</div>
</div>

<div id="downDiv" style="display:none">
</div>
</div>

<script language="javaScript">

	var pageNum = 1;
	var pageSize = 10;

	$().ready(function(){
		
		$('#win').window('close');

		var startYear = ${requestScope.versionYearList.startYear};
		var endYear = ${requestScope.versionYearList.endYear};
		
		for (var i=startYear; i<=endYear; i++){
			$("#startYear").append("<option>" + i + "</option>");
			$("#endYear").html($("#startYear").html());
		}
		
		$("#timeType").bind("change", function(){
			if (this.value=="2"){	//季报
				$("#quarter").show();
			}else{
				$("#quarter").hide();
			}
		});
	});
	
	function validate(){

		if ($("#timeType").val()==""){
			layer.alert("请选择核算频率");
			return;
		}
		
		if ($("#startYear").val()==""){
			layer.alert("请选择起始时间");
			return;
		}		
		
		$("#areaId").val("${sessionScope.user.userAreaId}");
		
		if ($("#areaId2").val()!="" && $("#areaId2").val() != undefined){
			$("#areaId").val($("#areaId2").val());
		}
		
		if ($("#areaId3").val()!="" && $("#areaId3").val() != undefined){
			$("#areaId").val($("#areaId3").val());
		}
		return true;
	}
		
	//表单提交查询
	function queryData(){

		if (!validate()){
			return;
		}
		
		var timeYear;
		var quarter;
		
		if ($("#timeType").val()=="1"){
			timeYear = 2;
		}else if ($("#timeType").val()=="3"){
			timeYear = 1;
		}else if ($("#timeType").val()=="2"){
			quarter = $("#quarter").val();			
		}
		
		
		var quarter;
		
		var url = "${baseURL}/audit/indicatorAccount";
		var params = {pageNum:pageNum, timeYear:timeYear, timeType:$("#timeType").val(), quarter:quarter, areaId:$("#areaId").val(), year:$("#startYear").val(), category2:$("#endYear").val()};
		$('#dg').datagrid({
		    url:url,
		    pagination:true,
		    pageSize:pageSize,
		    rownumbers:true,
		    queryParams:params,
		    columns:[[
		        {field:'year',width:80,align:'center',title:'年份'},
		        {field:'indicatorName',width:150, title:'指标名称',styler:function(value,row,index){
		        	if (row.isSummaryIndicator==1){
						return 'font-weight:bold';
					}
		        }},   
		        {field:'indicatorValue',width:120,align:'right',title:'指标值'}, 
		        {field:'editValue',width:120,align:'right',title:'修正值', styler:function(value,row,index){
		        	if (row.indicatorValue!=row.editValue){
						return 'color:red;';
					}
		        }}, 
		        {field:'indicatorUnit',width:80,align:'right',title:'单位'}, 
		        {field:'indicatorValueGrowthRate',width:120,align:'right',sortable:true,title:'指标值增长速度'}, 
		        {field:'editValueGrowthRate',width:120,align:'right',sortable:true,title:'指修正增长速度'}, 
		        {field:'ivYearBookGrowthRate',width:120,align:'right',sortable:true,title:'指标值偏离值'}, 
		        {field:'evYearBookGrowthRate',width:120,align:'right',sortable:true,title:'修正值偏离值'}, 
		        {field:'ivLastYearBookGrowthRate',width:120,align:'right',sortable:true,title:'指标值相比增长速度'}, 
		        {field:'evLastYearBookGrowthRate',width:120,align:'right',sortable:true,title:'修正值相比增长速度'},
		        {field:'operate',width:120,align:'center',title:'操作'},
		        {field:'indicatorId',hidden:true,title:'操作'}
		    ]],
		    loadFilter: function(data){
		    	data.rows = data.operationResult;
		    	data.total = data.totalCount;
		    	
		    	$.each(data.operationResult, function(i, row){
		    		
		    		row.operate = "<a href='#' onclick='showIndicatorProcess(" + i + ");return false;'>详情</a>&nbsp;&nbsp;";
		    		
		    		if (row.isSummaryIndicator!=1&&"${user.roleId}"!="6"){		//总值不可修改操作
			    		row.operate = row.operate + "<a href='#' onclick='modEditValue(" + i + "); return false;'>修改</a>"
			    		+ "&nbsp;&nbsp;<a href='#' onclick='calculateSingleIndicator(" + i + ");return false;'>核算</a>";
		    		}
		    		
		    	});
		    	
		    	
		    	return data;
			}
		}); 
	}

	
	var row_obj;
	
	var modHtml;
	
	var rowIndex;
	
	//加载修正值修改窗口
	function modEditValue(index){
		
		rowIndex = index;
		$("#dg").datagrid("selectRow", index);	//先选中需要修改的行，再获取行信息
		row_obj = $("#dg").datagrid("getSelected", index);	//根据行索引获取行信息
		modHtml = $("#modDiv").html();

		$.layer({
			type:1,
			title:'修改' + row_obj.indicatorName,
			area: ['400px', '250px'],
			page:{html:modHtml},
			end: function(){
				$("#modDiv").html(modHtml);
			}
		});
		
		$("#modDiv").empty();
		
		$("#indicatorName").text(row_obj.indicatorName);
		$("#indicatorValue").text(row_obj.indicatorValue);
		$("#editValue").val(row_obj.editValue);
		
		$("#" + row_obj.areaId).css("color", "red");
		$("#" + row_obj.areaId).css("font-weight", "200");
		
		queryIndicatorAccount();
		
		$("#editValue").keyup(function(){
			$("#" + row_obj.areaId).text($(this).val());
			checkEditValue();
		});
	}
	
	//查询全省指标值
	function queryIndicatorAccount(){
		
		var quarter;

		if (row_obj.timeType=="2"){	//如果为季报，获取季度信息
			quarter = $("#quarter").val();			
		}
		
		var data = {
			indicatorId:row_obj.indicatorId,
			timeType:row_obj.timeType,
			year:row_obj.year,
			month:row_obj.month,
			yearTime:row_obj.yearTime,
			quarter:row_obj.quarter,
			createPreId:'${sessionScope.user.id}'
		};
		
		var url = "${baseURL}/audit/queryIndicatorAccount";

		$.post(url, data, function(result){
			
			$.each(result, function(i, obj){
				$("#" + obj.areaId).text(obj.editValue);
			});
			
			checkEditValue();
			
		}, 'json');
	}
	
	//校验沿海三市的修正值 和 全省修正值 比较
	function checkEditValue(){
		var ret = parseFloat($("#320000000000000").text()) >= (parseFloat($("#320600000000000").text()) + parseFloat($("#320700000000000").text()) + parseFloat($("#320900000000000").text()));
		if (!ret){
			$("#editValue").css("background-color", "#FFC78E");
		}else{
			$("#editValue").css("background-color", "");
		}
		return ret;
	}
	
	
	//保存修正值方法
	function saveEditValue(){

		var editValue = $("#editValue").val();
		
		var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
		
		if (!exp.test(editValue)){
			layer.alert("请输入正确格式的数字，并且只能有两位小数");
			return;
		}
		
		if (!checkEditValue()){
			
			layer.confirm("沿海三市的修正值总和已大于全省修正值，是否确认保存？", saveSubmitEditValue);
			
		}else{

			saveSubmitEditValue();
		}

	}
	
	function saveSubmitEditValue(){
		
		var editValue = $("#editValue").val();
		
		var quarter;

		if (row_obj.timeType=="2"){	//如果为季报，获取季度信息
			quarter = $("#quarter").val();			
		}
		
		var data = {
			id:row_obj.id,
			areaId:row_obj.areaId,
			timeType:row_obj.timeType,
			year:row_obj.year,
			month:row_obj.month,
			yearTime:row_obj.yearTime,
			quarter:row_obj.quarter,
			editValue:editValue,
			createPreId:'${sessionScope.user.id}'
		};
		
		var url = "${baseURL}/audit/updateIndicatorAccount";
		
		$.post(url, data, function(msg){
			layer.msg("保存成功。", 1, 1, function(){
				
				$("#dg").datagrid("updateRow", {index:rowIndex, row:{editValue:editValue}});
				
				winClose();
			});
		}, 'json');
	}
	
	
	function winClose(){
		layer.closeAll();	
	}
	
	//生成核算结果
	function indicatorAccount(){
		if (!validate()){
			return;
		}
		
		var yearTime;
		var quarter;
		
		var params = "timeType=" + $("#timeType").val() + "&areaId=" + $("#areaId").val() + "&year=" + $("#startYear").val();
		
		if ($("#timeType").val()=="1"){
			yearTime = 2;
		}else if ($("#timeType").val()=="3"){
			yearTime = 1;
		}else if ($("#timeType").val()=="2"){
			params = params + "&quarter=" + $("#quarter").val();
		}
		
		params = params +  "&yearTime=" + yearTime;
		
		var url = "${baseURL}/audit/initIndicatorAccount";
		

		$.ajax({
			url: url,
			type: 'post',
			dataType: 'json',
			data : params,
			beforeSend: function(){
				loadi = layer.load('数据生成中…');
			},
			complete: function(){
				layer.close(loadi);
			},
			success: function(msg){
				if (msg=="1"){
					queryData();  //显示查询结果
				}
			}
		});
		

	}
	
	function queryAreasByParent(obj){
		$.getJSON("${baseURL}/area/queryAreasByParent/" + obj.value, function(json){
			$("#areaId3").empty();
			$("#areaId3").append("<option value=''>请选择..</option>");
			$.each(json, function(i, area){
				$("#areaId3").append("<option value='" + area.id + "'>" + area.areaName + "</option>");
			});
		});
	}
	
	//生成单条核算结果
	function calculateSingleIndicator(index){
		
		$("#dg").datagrid("selectRow", index);	//先选中需要修改的行，再获取行信息
		var row = $("#dg").datagrid("getSelected", index);	//根据行索引获取行信息
		
		var url = "${baseURL}/audit/calculateSingleIndicator";
		var data = {id:row.id};
		$.post(url, data, function(msg){
			if (msg=="1"){
				layer.msg("核算结果生成", 1, 1, function(){
					$('#dg').datagrid('reload'); //刷新查询结果
				});
			}
		}, 'json');
	}
	
	
	function showIndicatorProcess(index){

		$("#dg").datagrid("selectRow", index);	//先选中需要修改的行，再获取行信息
		var row = $("#dg").datagrid("getSelected", index);	//根据行索引获取行信息
		
		var url = '${baseURL}/audit/indicatorProcess/'+row.id;
		openWin(row.indicatorName, url, $(window).width(), $(parent.window).height()-50);
	}
	
	function downIndicator(){
		var url = "${baseURL}/indicator-stat.do?method=account";
		
		var year = $("#startYear").val();
		var yearTime = $("#endYear").val();
		
		if (year==""){
			layer.alert("请选择起始时间");
			return;
		}
		if (yearTime==""){
			layer.alert("请选择结束时间");
			return;
		}
		if (Number(year) > Number(yearTime)){
			layer.alert("请正确的时间范围");
			return;
		}
		
		url = url + "&year=" + $("#startYear").val() + "&yearTime=" + $("#endYear").val();
		
		var iframe = document.createElement("iframe");
		iframe.src = url;
		document.getElementById("downDiv").appendChild(iframe);
	}
	
</script>




<%@ include file="/static/include/bottom.jsp"%>